{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f04dd603-a2d1-48ce-8c17-9f1dba8de1ee",
   "metadata": {},
   "source": [
    "Chapter 01\n",
    "\n",
    "# 向量内积\n",
    "《线性代数》 | 鸢尾花书：数学不难"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ccafb456-2453-4c82-8a65-b1963a370cb2",
   "metadata": {},
   "source": [
    "在这段代码中，我们计算了两个向量 $a_{\\text{vec}}$ 和 $b_{\\text{vec}}$ 的**内积（点积）**，并验证了计算过程。\n",
    "\n",
    "---\n",
    "\n",
    "### **向量的定义**\n",
    "首先，代码使用 NumPy 进行向量运算，并定义两个三维向量：\n",
    "$$\n",
    "a_{\\text{vec}} = \\begin{bmatrix} 1 \\\\ 2 \\\\ 3 \\end{bmatrix}, \\quad\n",
    "b_{\\text{vec}} = \\begin{bmatrix} 4 \\\\ 5 \\\\ 6 \\end{bmatrix}\n",
    "$$\n",
    "它们表示两个三维列向量。\n",
    "\n",
    "---\n",
    "\n",
    "### **向量内积的定义**\n",
    "向量的**内积（点积）**定义为：\n",
    "$$\n",
    "a_{\\text{vec}} \\cdot b_{\\text{vec}} = \\sum_{i=1}^{n} a_i b_i\n",
    "$$\n",
    "其中，$a_i$ 和 $b_i$ 是两个向量在第 $i$ 维的分量。\n",
    "\n",
    "对于三维向量：\n",
    "$$\n",
    "a_{\\text{vec}} \\cdot b_{\\text{vec}} = (1 \\times 4) + (2 \\times 5) + (3 \\times 6)\n",
    "= 4 + 10 + 18 = 32\n",
    "$$\n",
    "\n",
    "---\n",
    "\n",
    "### **代码计算**\n",
    "代码使用 NumPy 计算内积：\n",
    "```python\n",
    "a_dot_b = np.dot(a_vec, b_vec)\n",
    "```\n",
    "这相当于直接计算公式：\n",
    "$$\n",
    "\\text{np.dot}(a_{\\text{vec}}, b_{\\text{vec}}) = a_{\\text{vec}} \\cdot b_{\\text{vec}} = 32\n",
    "$$\n",
    "\n",
    "同时，代码还进行了手动计算：\n",
    "```python\n",
    "a_vec * b_vec\n",
    "```\n",
    "这返回一个逐元素相乘的向量：\n",
    "$$\n",
    "a_{\\text{vec}} * b_{\\text{vec}} =\n",
    "\\begin{bmatrix} 1 \\times 4 \\\\ 2 \\times 5 \\\\ 3 \\times 6 \\end{bmatrix}\n",
    "=\\begin{bmatrix} 4 \\\\ 10 \\\\ 18 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "最后，代码使用 `sum()` 计算这些元素的和：\n",
    "```python\n",
    "sum(a_vec * b_vec)\n",
    "```\n",
    "$$\n",
    "\\sum \\begin{bmatrix} 4 \\\\ 10 \\\\ 18 \\end{bmatrix} = 4 + 10 + 18 = 32\n",
    "$$\n",
    "这与 `np.dot()` 计算的结果一致，验证了点积计算的正确性。\n",
    "\n",
    "---\n",
    "\n",
    "### **总结**\n",
    "- **`np.dot(a_vec, b_vec)`** 直接计算了向量的内积，得到 $32$。\n",
    "- **`a_vec * b_vec`** 计算了向量的逐元素乘积，返回向量 $[4, 10, 18]$。\n",
    "- **`sum(a_vec * b_vec)`** 计算了逐元素乘积的和，验证了点积公式的正确性。\n",
    "\n",
    "最终，代码通过两种方式计算并验证了向量内积的正确性，确保了计算过程符合数学定义。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "26e6e3a3-18f5-4574-8d51-4285a44fedc6",
   "metadata": {},
   "source": [
    "## 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4c82adc5-d585-437e-9f61-f176964aff4d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3b3883ee-271d-456e-ba12-fc5e19506ac0",
   "metadata": {},
   "source": [
    "## 定义两个向量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2f98e172-8523-4906-a9c5-b27315132cc0",
   "metadata": {},
   "outputs": [],
   "source": [
    "a_vec = np.array([1, 2, 3])\n",
    "b_vec = np.array([4, 5, 6])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5e950e08-288e-43e9-8537-f7ec0d7f1276",
   "metadata": {},
   "source": [
    "## 计算内积"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "45b94d14-8b23-4f8d-aa1b-076c77df72ad",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_dot_b = np.dot(a_vec, b_vec)\n",
    "a_dot_b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c477a80b-9ba5-43d3-a649-50aedd05beb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 4, 10, 18])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a_vec * b_vec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "954b7525-dcef-47cb-af13-40c741ca3891",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "32"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum(a_vec * b_vec)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "293d22ed-2b5b-4c4b-a60e-4825096fad45",
   "metadata": {},
   "source": [
    "作者\t**生姜DrGinger**  \n",
    "脚本\t**生姜DrGinger**  \n",
    "视频\t**崔崔CuiCui**  \n",
    "开源资源\t[**GitHub**](https://github.com/Visualize-ML)  \n",
    "平台\t[**油管**](https://www.youtube.com/@DrGinger_Jiang)\t\t\n",
    "\t\t[**iris小课堂**](https://space.bilibili.com/3546865719052873)\t\t\n",
    "\t\t[**生姜DrGinger**](https://space.bilibili.com/513194466)  "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
